const fs = require('fs');
const path = require('path');

module.exports = function (pagePath, name, text) {
  const componentPath = pagePath + '/component';
  if (!fs.existsSync(componentPath)) {
    fs.mkdirSync(componentPath);
  }
  const componentName = `${name}`;

  const componentFile = componentPath + `/${componentName}.js`;
  if (!fs.existsSync(componentFile)) {
    const template = [];
    // template.push(`import * as React from 'react';`);
    template.push(`import React, { Component } from 'react-native';`);
    template.push(`import propTypes from 'prop-types';`);
    template.push(`import { Map } from 'immutable';`);
    template.push(`import BaseComponent from '../../../lib';`);
    template.push(``);
    template.push(`class ${componentName} extends Component {`);
    // template.push(`  constructor(props) {`);
    // template.push(`    super(props);`);
    // template.push(`  }`);
    template.push(`  render() {`);
    template.push(`    return (`);
    template.push(`      <div>${text || '页面名称'}</div>`);
    template.push(`    );`);
    template.push(`  }`);
    template.push(`}`);
    template.push(``);
    template.push(`/** 定义propTypes */`);
    template.push(`${componentName}.propTypes = {`);
    template.push(`  /** 属性 */`);
    template.push(`  props: propTypes.oneOfType(Map).isRequired,`);
    template.push(`  /** 方法 */`);
    template.push(`  func: propTypes.func.isRequired,`);
    template.push(`};`);
    template.push(``);
    template.push(`export default ${componentName};`);
    template.push(``);
    fs.writeFileSync(componentFile, template.join('\n'));
  }
}
